计算两点位置的距离,返回两点的距离,单位:米,误差小于0.2米
<param name="lat1">第一点纬度</param>
<param name="lng1">第一点经度</param>
<param name="lat2">第二点纬度</param>
<param name="lng2">第二点经度</param>
double GetDistanceByLatLng(double lat1, double lng1, double lat2, double lng2)
#define MATH_PI 3.1415926
#define EARTH_RADIUS 6378137 /*地球近似半径m*/static double math_radian(double d)
{
return d * MATH_PI / 180.0;
}double GetDistanceByLatLng(double lat1, double lng1, double lat2, double lng2)
{
double radLat1 = math_radian(lat1);
double radLng1 = math_radian(lng1);
double radLat2 = math_radian(lat2);
double radLng2 = math_radian(lng2);
double a = radLat1 - radLat2;
double b = radLng1 - radLng2;
double result = 2 * asin(sqrt(pow(sin(a / 2), 2) cos(radLat1) * cos(radLat2) * pow(sin(b / 2), 2))) * EARTH_RADIUS;
return result;
}
评论